package com.ruoyi.web.controller.danger;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDangerUnit;
import com.ruoyi.system.domain.vo.DangerCheckDeptVO;
import com.ruoyi.system.service.ISysDangerUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/danger/unit")
public class SysDangerUnitController extends BaseController {

    @Autowired
    private ISysDangerUnitService sysDangerUnitService;

    @PostMapping("/create")
//    @PreAuthorize("@ss.hasPermi('danger:unit:create')")
    public AjaxResult create(@RequestBody SysDangerUnit sysDangerUnit) {
        sysDangerUnitService.create(sysDangerUnit);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public TableDataInfo list(SysDangerUnit sysDangerUnit) {
        startPage();
        List<SysDangerUnit> sysDangerUnits = sysDangerUnitService.list(sysDangerUnit);
        return getDataTable(sysDangerUnits);
    }

    @GetMapping("/delete")
//    @PreAuthorize("@ss.hasPermi('danger:unit:delete')")
    public AjaxResult delete(Integer id) {
        sysDangerUnitService.delete(id);
        return AjaxResult.success();
    }

    @PostMapping("/update")
//    @PreAuthorize("@ss.hasPermi('danger:unit:update')")
    public AjaxResult update(@RequestBody SysDangerUnit sysDangerUnit) {
        sysDangerUnitService.update(sysDangerUnit);
        return AjaxResult.success();
    }

    @GetMapping("/getAllDept")
    public AjaxResult getAllDept() {
        List<DangerCheckDeptVO> dangerCheckDeptVOS = sysDangerUnitService.getAllDept();
        return AjaxResult.success(dangerCheckDeptVOS);
    }

    @Log(title = "重大危险源单元维护(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysDangerUnit> util = new ExcelUtil<>(SysDangerUnit.class);
        List<SysDangerUnit> sysDangerUnits = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysDangerUnitService.importExcel(sysDangerUnits, updateSupport, operName);
        return success(message);
    }


    @Log(title = "重大危险源单元维护(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDangerUnit sysDangerUnit)
    {
        List<SysDangerUnit> sysDangerUnits = this.sysDangerUnitService.list(sysDangerUnit);
        ExcelUtil<SysDangerUnit> util = new ExcelUtil<>(SysDangerUnit.class);
        util.exportExcel(response, sysDangerUnits, "重大危险源单元维护(");
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysDangerUnit> util = new ExcelUtil<>(SysDangerUnit.class);
        util.importTemplateExcel(response,"重大危险源单元维护(");
    }
}
